package org.devbg.academy.corejava.shop.ui.frames;


import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

import org.devbg.academy.corejava.shop.db.daos.CategoryDAO;
import org.devbg.academy.corejava.shop.db.entities.Category;
import org.devbg.academy.corejava.shop.db.utils.HibernateSessionManager;
import org.devbg.academy.corejava.shop.ui.models.CategoryExpandListener;
import org.devbg.academy.corejava.shop.ui.models.CategoryTreeNode;
import org.devbg.academy.corejava.shop.ui.models.OpenOrdersDialogListener;
import org.devbg.academy.corejava.shop.ui.models.ProductTableModel;

public class ShopFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JMenuBar shopMenuBar = null;
	private JMenu fileMenu = null;
	private JMenuItem nomeclaturesMenuItem = null;
	private JMenuItem ordersMenuItem = null;
	private JMenuItem exitMenuItem = null; // @jve:decl-index=0:
	private JSplitPane nomenclaturesSplitPane = null;
	private JScrollPane categoriesTreeScrollPane = null;
	private JTree categoriesTree = null;
	private JScrollPane productsScrollPane = null;
	private JTable productsTable = null;
	private DefaultMutableTreeNode categoryRootTreeNode = null; // @jve:decl-index=0:visual-constraint="-12,142"
	private CategoryExpandListener categoryExpandListener = null; // @jve:decl-index=0:visual-constraint="-7,93"
	private ProductTableModel productTableModel = null; // @jve:decl-index=0:visual-constraint="974,55"
	private OpenOrdersDialogListener ordersDialogListener = null;
	
	/**
	 * This is the default constructor
	 */
	public ShopFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		HibernateSessionManager.openSession();
		this.setSize(820, 330);
		this.setJMenuBar(getShopMenuBar());
		this.setContentPane(getNomenclaturesSplitPane());
		this.setTitle("Магазин");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosed(java.awt.event.WindowEvent e) {
				HibernateSessionManager.closeSession();
			}
		});
	}

	/**
	 * This method initializes shopMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getShopMenuBar() {
		if (shopMenuBar == null) {
			shopMenuBar = new JMenuBar();
			shopMenuBar.add(getFileMenu());
		}
		return shopMenuBar;
	}

	/**
	 * This method initializes FileMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("Меню");
			fileMenu.add(getNomeclaturesMenuItem());
			fileMenu.add(getOrdersMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes nomeclaturesMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getNomeclaturesMenuItem() {
		if (nomeclaturesMenuItem == null) {
			nomeclaturesMenuItem = new JMenuItem();
			nomeclaturesMenuItem.setText("Номенклатури");
		}
		return nomeclaturesMenuItem;
	}

	private JMenuItem getOrdersMenuItem() {
		if (ordersMenuItem == null) {
			ordersMenuItem = new JMenuItem();
			ordersMenuItem.setText("Поръчки");
			ordersMenuItem.addActionListener(getOrdersDialogListener());
		}
		return ordersMenuItem;
	}

	/**
	 * This method initializes exitMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Изход");
		}
		return exitMenuItem;
	}
	
	/**
	 * This method initializes nomenclaturesSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getNomenclaturesSplitPane() {
		if (nomenclaturesSplitPane == null) {
			nomenclaturesSplitPane = new JSplitPane();
			nomenclaturesSplitPane
					.setLeftComponent(getCategoriesTreeScrollPane());
			nomenclaturesSplitPane.setRightComponent(getProductsScrollPane());
		}
		return nomenclaturesSplitPane;
	}

	/**
	 * This method initializes categoriesTreeScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getCategoriesTreeScrollPane() {
		if (categoriesTreeScrollPane == null) {
			categoriesTreeScrollPane = new JScrollPane();
			categoriesTreeScrollPane.setViewportView(getCategoriesTree());
		}
		return categoriesTreeScrollPane;
	}

	/**
	 * This method initializes categoriesTree
	 * 
	 * @return javax.swing.JTree
	 */
	private JTree getCategoriesTree() {
		if (categoriesTree == null) {
			DefaultTreeModel model = new DefaultTreeModel(
					getCategoryRootTreeNode());
			categoriesTree = new JTree(model);
			categoriesTree
					.addTreeWillExpandListener(getCategoryExpandListener());
			categoriesTree
					.addTreeSelectionListener(new TreeSelectionListener() {

						public void valueChanged(TreeSelectionEvent event) {
							Object lastElement = event.getPath()
									.getLastPathComponent();
							if (lastElement instanceof CategoryTreeNode) {
								CategoryTreeNode node = (CategoryTreeNode) lastElement;
								getProductTableModel().changeCategory(
										node.getCategory());
							}
						}

					});
			categoriesTree.setEditable(true);
			categoriesTree.getSelectionModel().setSelectionMode(
					TreeSelectionModel.SINGLE_TREE_SELECTION);
			categoriesTree.setShowsRootHandles(true);
		}
		return categoriesTree;
	}

	/**
	 * This method initializes productsScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getProductsScrollPane() {
		if (productsScrollPane == null) {
			productsScrollPane = new JScrollPane();
			productsScrollPane.setViewportView(getProductsTable());
		}
		return productsScrollPane;
	}

	/**
	 * This method initializes productsTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getProductsTable() {
		if (productsTable == null) {
			productsTable = new JTable(getProductTableModel());
		}
		return productsTable;
	}

	/**
	 * This method initializes categoryRootTreeNode
	 * 
	 * @return javax.swing.tree.DefaultMutableTreeNode
	 */
	private DefaultMutableTreeNode getCategoryRootTreeNode() {
		if (categoryRootTreeNode == null) {
			categoryRootTreeNode = new DefaultMutableTreeNode();
			categoryRootTreeNode.setUserObject("Категории");
			List<Category> rootCategories = new CategoryDAO()
					.findRootEntities();
			for (Category category : rootCategories) {
				CategoryTreeNode rootCategory = new CategoryTreeNode(category);
				categoryRootTreeNode.add(rootCategory);
			}
		}
		return categoryRootTreeNode;
	}

	/**
	 * This method initializes categoryExpandListener
	 * 
	 * @return org.devbg.academy.corejava.shop.ui.models.CategoryExpandListener
	 */
	private CategoryExpandListener getCategoryExpandListener() {
		if (categoryExpandListener == null) {
			categoryExpandListener = new CategoryExpandListener();
		}
		return categoryExpandListener;
	}

	/**
	 * This method initializes productTableModel
	 * 
	 * @return org.devbg.academy.corejava.shop.ui.models.ProductTableModel
	 */
	private ProductTableModel getProductTableModel() {
		if (productTableModel == null) {
			productTableModel = new ProductTableModel();
		}
		return productTableModel;
	}
	
	private OpenOrdersDialogListener getOrdersDialogListener() {
		if (ordersDialogListener == null) {
			ordersDialogListener = new OpenOrdersDialogListener();
		}
		return ordersDialogListener;
	}

} // @jve:decl-index=0:visual-constraint="145,22"
